package com.bit.forum.controller;

import com.bit.forum.common.AppResult;
import com.bit.forum.common.ResultCode;
import com.bit.forum.exception.ApplicationException;
import com.bit.forum.model.Board;
import com.bit.forum.service.IBoardService;
import jakarta.annotation.Resource;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/board")
public class BoardController {
    @Resource
    private IBoardService boardService;

    @Value("${forum.index.board-num}")
    private Integer boardNum;

    @GetMapping("/topList")
    public AppResult<List<Board>> topList() {
        log.info("首页板块个数为:"+boardNum);
        List<Board> boards = boardService.selectByNum(boardNum);
        //判断是否为空
        if(boards==null||boards.size()==0){
            boards=new ArrayList<>();
        }
        return AppResult.success(boards);
    }

    @GetMapping("/getById")
    public AppResult<Board> getById(@RequestParam("id") @NonNull Long id) {
        Board board = boardService.selectById(id);
        if(board==null||board.getDeleteState()==1){
            //打印日志
            log.warn(ResultCode.ERROR_BOARD_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_BOARD_NOT_EXISTS));
        }
        return AppResult.success(board);
    }
}
